package com.jcl.recruit.console.core.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.jcl.core.db.DynamicDataSource;
import com.jcl.core.db.config.DataSourceConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @ClassName DataSourceConfig
 * @Author yyliu
 * @Date 2018/12/18 10:56
 * @Version 1.0
 **/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.jcl.recruit.**.mapper"})
public class DataSourceConfig {

    static final String PREFIX = "spring.datasource";
    static final String PACKAGE = "com.jcl.recruit.**.model";
    static final String MAPPER_LOCATION = "/mybatis-config.xml";

    @Autowired
    private Environment env;

    @Bean
    public DataSourceConfiguration dataSourceConfiguration() {
        return new DataSourceConfiguration(PREFIX, PACKAGE, MAPPER_LOCATION, env);
    }

    /**
     * 创建默认数据源
     */
    @Bean
    public DataSource consoleDataSource(@Qualifier("dataSourceConfiguration")
                                                DataSourceConfiguration dsc) throws Exception {
        return dsc.consoleDataSource();
    }

    /**
     * @Primary 该注解表示在同一个接口有多个实现类可以注入的时候，默认选择哪一个，而不是让@autowire注解报错
     * @Qualifier 根据名称进行注入，通常是在具有相同的多个类型的实例的一个注入（例如有多个DataSource类型的实例）
     */
    @Bean
    @Primary
    public DynamicDataSource dynamicDataSource(@Qualifier("consoleDataSource") DataSource consoleDataSource,
                                               @Qualifier("dataSourceConfiguration")
                                                       DataSourceConfiguration dsc) {
        return dsc.dataSource(consoleDataSource);
    }

    /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DynamicDataSource ds,
                                               @Qualifier("dataSourceConfiguration")
                                                       DataSourceConfiguration dsc) throws Exception {
        return dsc.sqlSessionFactory(ds);
    }

    /**
     * 配置事务管理器
     */
    @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DynamicDataSource ds,
                                                           @Qualifier("dataSourceConfiguration")
                                                                   DataSourceConfiguration dsc) throws Exception {
        return dsc.transactionManager(ds);
    }

    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //添加初始化参数：initParams
        //白名单：
        // servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        // servletRegistrationBean.addInitParameter("allow", "test.jucailin.com");
        servletRegistrationBean.addInitParameter("allow", "localhost");
        //IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        //servletRegistrationBean.addInitParameter("deny","192.168.1.73");
        //登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * druid过滤器
     * 注册一个：filterRegistrationBean
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
